package com.xhu.rjsx2.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xhu.rjsx2.enums.AppHttpCodeEnum;
import com.xhu.rjsx2.pojo.Member;
import com.xhu.rjsx2.pojo.ResponseResult;
import com.xhu.rjsx2.pojo.User;
import com.xhu.rjsx2.pojo.dto.MemberDTO;
import com.xhu.rjsx2.pojo.vo.LayuiPageResult;
import com.xhu.rjsx2.pojo.vo.MemberVO;
import com.xhu.rjsx2.pojo.vo.PageVO;
import com.xhu.rjsx2.service.MemberService;
import com.xhu.rjsx2.util.BeanCopyUtils;
import io.swagger.annotations.*;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author xhu-zfx
 * @email <756867768@qq.com>
 * @date 2023/5/8 23:27
 * @description
 */

@RequestMapping("/member")
@RestController
@Api(tags = "会员（member）管理接口API",description = "Member Controller")
public class MemberController {

    @Resource
    private MemberService memberService;

    @GetMapping("/listAllMember")
    @ApiOperation(value = "查询所有数据接口")
    public ResponseResult listAllMember(){

        List<Member> memberList = memberService.list();
        List<MemberVO> memberVOList = BeanCopyUtils.copyBeanList(memberList, MemberVO.class);
        return ResponseResult.okResult(memberVOList);
    }

    @GetMapping("/listAllMemberPage")
    @ApiOperation(value = "列表分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数"),
            @ApiImplicitParam(name = "limit", value = "每页条数"),
            @ApiImplicitParam(name = "memberName", value = "会员姓名"),
            @ApiImplicitParam(name = "memberPhone", value = "电话号码"),
            @ApiImplicitParam(name = "memberStartTime", value = "生日范围-起始时间"),
            @ApiImplicitParam(name = "memberEndTime", value = "生日范围-结束时间")
    })
    public LayuiPageResult listAllMemberPage(Integer page, Integer limit, String memberName, String memberPhone, Date memberStartTime,Date memberEndTime){

        PageVO pageVO = memberService.listAllMemberPage(page,limit,memberName,memberPhone,memberStartTime,memberEndTime);
        return new LayuiPageResult(LayuiPageResult.CODE_SUCCESS,LayuiPageResult.MESSAGE_SUCCESS,pageVO.getRows(), pageVO.getTotal());
    }


    @PostMapping("/updateMemberById")
    @ApiOperation(value = "通过主键更新会员信息")
    public ResponseResult updateMemberById(@ApiParam(value = "会员传输对象",required = true) @RequestBody MemberDTO memberDTO){
        Member member = BeanCopyUtils.copyBeanSingle(memberDTO, Member.class);
        boolean updateSuccess = memberService.updateById(member);
        return updateSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
    @PostMapping("/deleteMemberById")
    @ApiOperation(value = "通过主键删除会员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "当前会员id")
    })
    public ResponseResult deleteMemberById(Integer id){
        boolean removeSuccess = memberService.removeById(id);
        return removeSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
    @PostMapping("/insertMember")
    @ApiOperation(value = "新增会员接口")
    public ResponseResult insertMember(@ApiParam(value = "会员对象",required = true) @RequestBody MemberDTO memberDTO){
        // 禁止用户传入id，由系统生成
        memberDTO.setId(null);
        Member member = BeanCopyUtils.copyBeanSingle(memberDTO, Member.class);
        member.setMemberNumber(IdUtil.simpleUUID());
        boolean saveSuccess = memberService.save(member);
        return saveSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

}
